package com.arpa.wms.controller.wms;
import com.arpa.core.web.MediaTypes;
import com.arpa.core.web.Result;
import com.arpa.ntocc.common.common.Page;
import com.arpa.ntocc.common.common.constant.StrConsts;
import com.arpa.ntocc.common.common.util.UserUtil;
import com.arpa.ntocc.common.security.annotation.NeedLogin;
import com.arpa.wms.domain.dto.AllocationItemDTO;
import com.arpa.wms.domain.entity.Allocation;
import com.arpa.wms.domain.entity.AllocationItem;
import com.arpa.wms.domain.vo.AllocationItemVO;
import com.arpa.wms.service.IAllocationItemService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.*;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * <p>
 * WMS调拨单明细 Controller
 * </p>
 *
 * @author LEO
 * @since 2020-11-17
 */
@Api(tags = "WMS调拨单明细")
@RestController
@RequestMapping(value="/allocationItem", produces = "application/json;charset=UTF-8")
@Log4j2
public class AllocationItemController {

   @Autowired
   private IAllocationItemService  iAllocationItemService;

    /**
     * 获取 WMS调拨单明细 列表
     *
     * @param allocationItemDTO
     * @param request
     * @return
     */
    @ApiOperation(value = "获取 WMS调拨单明细 列表", notes = "获取 WMS调拨单明细 列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = AllocationItemVO.class))
    @NeedLogin
    @GetMapping()
    public Result list(AllocationItemDTO allocationItemDTO,HttpServletRequest request) {
        try {
            allocationItemDTO.setGroupCode(UserUtil.getBranchCode());
            AllocationItemVO allocationItemVoSum = iAllocationItemService.queryListSum(allocationItemDTO);
            List<AllocationItemVO> allocationItemVOList = iAllocationItemService.queryList(allocationItemDTO);
            Page returnPage = new Page(allocationItemDTO.getPageNum(),allocationItemDTO.getPageSize());
            returnPage.setRecords(allocationItemVOList);
            returnPage.setTotal(allocationItemVoSum.getTotal());
            returnPage.setSumObject(allocationItemVoSum);
            return Result.ok().data(returnPage);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

    /**
     * 添加 WMS调拨单明细
     *
     * @param entity AllocationItem
     * @param request
     * @return
     */
    @ApiOperation(value = "添加 WMS调拨单明细 ", notes = "添加 WMS调拨单明细 ")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @PostMapping()
    public Result create(AllocationItem entity, HttpServletRequest request) {
        try {
            iAllocationItemService.save(entity);
            return Result.ok("添加成功");
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

    /**
    * 获取WMS调拨单明细详情
    *
    * @param code
    * @param request
    * @return
    */
    @ApiOperation(value = "获取WMS调拨单明细详情", notes = "获取WMS调拨单明细详情")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer 123456"),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000"),
        @ApiImplicitParam(name = "code", value = "WMS调拨单明细code", paramType = "path", dataType = "String")
    })
    @ApiResponses(@ApiResponse(code = 0, message = "success", response = AllocationItem.class))
    @NeedLogin
    @GetMapping(value = "/{code}")
    public Result allocationItem(@PathVariable String code, HttpServletRequest request) {
        try {
            AllocationItem entity = iAllocationItemService.getOne(new QueryWrapper<AllocationItem>().lambda().eq(AllocationItem::getCode,code).eq(AllocationItem::getGroupCode,UserUtil.getBranchCode()));
            return Result.ok().data(entity);
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }


    /**
     * 修改WMS调拨单明细
     *
     * @param entity WMS调拨单明细
     * @param request
     * @return
     */
    @ApiOperation(value = "修改WMS调拨单明细", notes = "修改WMS调拨单明细")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000")
    })
    @NeedLogin
    @PutMapping()
    public Result update(AllocationItem entity,HttpServletRequest request) {
        if(StringUtils.isBlank(entity.getCode())){
            return Result.error("code不能为空");
        }
        int updateNum = iAllocationItemService.updateByCode(entity);
        if(updateNum == 0){
            return Result.error("没有更新数据！code:"+entity.getCode());
        }else{
            return Result.ok("修改成功,更新"+updateNum+"条数据！");
        }
    }

    /**
     * 删除 WMS调拨单明细
     *
     * @param codes
     * @param request
     * @return
     */
    @ApiOperation(value = "删除 WMS调拨单明细 ", notes = "删除 WMS调拨单明细 ", produces = MediaTypes.JSON_UTF_8)
    @ApiImplicitParams({
        @ApiImplicitParam(name = "Authorization", value = "授权token", required = true, paramType = "header", dataType = "String", defaultValue = "Bearer "),
        @ApiImplicitParam(name = "deviceId", value = "设备id", paramType = "header", dataType = "string", defaultValue = "0000000000"),
        @ApiImplicitParam(name = "codes", value = "codes", required = true, paramType = "query", dataType = "List")
    })
    @NeedLogin
    @DeleteMapping(value = "")
    public Result batchdel(@RequestParam List<String> codes, HttpServletRequest request) {
        try {
            iAllocationItemService.remove(new QueryWrapper<AllocationItem>().lambda().in(AllocationItem::getCode,codes).eq(AllocationItem::getGroupCode,UserUtil.getBranchCode()));
            return Result.ok();
        } catch (Exception e) {
            log.error(StrConsts.CATCH_MESSAGE,e);
            return Result.error(e.getMessage());
        }
    }

 }
